﻿var LogView = LogView || {};
LogView.Admin = LogView.Admin || {};

$.extend(LogView.Admin, function () {


    function deleteSite(siteId) {

        var response = LogView.MakeCall("Sites.aspx/Delete", { siteId: siteId });
        if (response.d.Status == "OK")
            LogView.UpdatePartial("Sites.aspx/ListSites", {}, "#listSites");
    }

    function updateAllFiles(siteId) {

        var files = $('tr[data-filepath] td[data-action=update]');
        if (files.length > 0) {

            updateFile(siteId, files.first().parent().attr('data-filepath'), function () { updateAllFiles(siteId) });
        }
    }

    function updateFile(siteId, filePath, complete) {

        $('tr[data-filepath="' + filePath + '"]').html("<td colspan=3>Processing</td>");
        $('tr[data-filepath="' + filePath + '"]').addClass('processing');

        var response = LogView.MakeCall("Files.aspx/Update", { siteId: siteId, filePath: filePath }, function (msg) {

            if (msg.d.Status == 'OK') {
                $('tr[data-filepath="' + filePath + '"]').removeClass('processing');
                $('tr[data-filepath="' + filePath + '"]').html(msg.d.Message);

                if (complete != undefined)
                    window.setTimeout(function () { complete(); }, 500);
            }
        });
    }

    return {

        DeleteSite: deleteSite,
        UpdateFile: updateFile,
        UpdateAllFiles: updateAllFiles    
    };

} ());

